package com.jmgd.cjlx.core;

import com.jmgd.cjlx.constants.MainConstant;
import com.jmgd.cjlx.service.WdzTokenService;
import com.ovopark.sdk.openplatform.GwInitRequestHandler;
import com.ovopark.sdk.openplatform.core.OvoParkHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WdzHttp {
    @Autowired
    WdzTokenService wdzTokenService;

    public String wdzhttp(GwInitRequestHandler requestHandler,String version,String function,String token){
        String resContent = "";

        //初始化 配置密匙密钥
        //GwInitRequestHandler requestHandler = new GwInitRequestHandler();
        //requestHandler.init();
        // 设置_aid，值为应用id
        requestHandler.setParameter("_aid", MainConstant.wdz_aid);
        requestHandler.setApplicationKey(MainConstant.wdz_akey);
        requestHandler.setApplicationSecret(MainConstant.wdz_asid);

        // 设置需要调用的方法名、请求地址、版本号
        requestHandler.setMethod(function);
        requestHandler.setGateUrl(MainConstant.wdz_apigwUrl);
        requestHandler.setParameter("_version", version);


        // 初始化httpClient
        OvoParkHttpClient httpClient = new OvoParkHttpClient();


        // 设置key和value，key为authenticator，value为获取到的token
        httpClient.setHeaders("authenticator",token);

        //获取请求带参数的url
        String requestUrl = requestHandler.getRequestURL();

        //设置请求内容
        httpClient.setReqContent(requestUrl);
        System.out.println("1.请求："+requestUrl);
        if (httpClient.call()) {
            resContent = httpClient.getResContent();
            System.out.println("2.返回："+resContent);
        }
        return resContent;
    }

}
